package com.tongzhan.service;

import com.tongzhan.base.BaseCriteria;
import com.tongzhan.entity.screen.HalfYear;
import com.tongzhan.entity.screen.Month;
import com.tongzhan.entity.screen.Season;
import com.tongzhan.entity.screen.Year;
import com.tongzhan.repository.screen.HalfYearRepository;
import com.tongzhan.repository.screen.MonthRepository;
import com.tongzhan.repository.screen.SeasonRepository;
import com.tongzhan.repository.screen.YearRepository;
import com.tongzhan.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DateService {
    @Autowired
    private YearRepository yearRepository;
    @Autowired
    private HalfYearRepository halfYearRepository;
    @Autowired
    private SeasonRepository seasonRepository;
    @Autowired
    private MonthRepository monthRepository;

    public Map<String, Object> getYearData() {
        Map<String, Object> map = new HashMap<>();
        String sysYear = DateUtil.getSysYear();
        Criteria criteria = BaseCriteria.where("is_del").is(0);
        criteria.and("year").lte(Integer.valueOf(sysYear));
        List<Year> list = yearRepository.find(new Query().addCriteria(criteria).with(Sort.by(Sort.Direction.DESC, "year")), null);
        map.put("dataList",list);
        return map;
    }

    public Map<String, Object> getHalfYearData() {
        Map<String, Object> map = new HashMap<>();
        String sysYear = DateUtil.getSysYear();
        Criteria criteria = BaseCriteria.where("is_del").is(0);
        criteria.and("year").lte(Integer.valueOf(sysYear));
        List<HalfYear> list = halfYearRepository.find(new Query().addCriteria(criteria).with(Sort.by(Sort.Direction.DESC, "year")), null);
        map.put("dataList",list);
        return map;
    }

    public Map<String, Object> getSeasonData() {
        Map<String, Object> map = new HashMap<>();
        String sysYear = DateUtil.getSysYear();
        Criteria criteria = BaseCriteria.where("is_del").is(0);
        criteria.and("year").lte(Integer.valueOf(sysYear));
        List<Season> list = seasonRepository.find(new Query().addCriteria(criteria).with(Sort.by(Sort.Direction.DESC, "year")), null);
        map.put("dataList",list);
        return map;
    }

    public Map<String, Object> getMonthData() {
        Map<String, Object> map = new HashMap<>();
        String sysYear = DateUtil.getSysYear();
        Criteria criteria = BaseCriteria.where("is_del").is(0);
        criteria.and("year").lte(Integer.valueOf(sysYear));
        List<Month> list = monthRepository.find(new Query().addCriteria(criteria).with(Sort.by(Sort.Direction.DESC, "year")), null);
        map.put("dataList",list);
        return map;
    }
}
